/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.presentationappearance;

import org.openaecfm.model.presentationappearance.TextStyleTextModel;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class TextStyleTextModelIO extends
		AbstractEntityInstanceInputImpl<TextStyleTextModel> implements
		IEntityInstanceOutput<TextStyleTextModel> {
	private SizeSelectIO sizeSelectIO;

	public void setSizeSelectIo(SizeSelectIO sizeSelectIO) {
		this.sizeSelectIO = sizeSelectIO;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(TextStyleTextModel pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCTEXTSTYLETEXTMODEL", appDataManager.getName(pojo));

		// Set TextIndent
		if (pojo.getTextIndent() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(sizeSelectIO.exportSPF(pojo.getTextIndent()));
		}

		// Set TextAlign
		if (pojo.getTextAlign() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getTextAlign(),
					StringParameter.UTF_8));
		}

		// Set TextDecoration
		if (pojo.getTextDecoration() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getTextDecoration(),
					StringParameter.UTF_8));
		}

		// Set LetterSpacing
		if (pojo.getLetterSpacing() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(sizeSelectIO.exportSPF(pojo.getLetterSpacing()));
		}

		// Set WordSpacing
		if (pojo.getWordSpacing() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(sizeSelectIO.exportSPF(pojo.getWordSpacing()));
		}

		// Set TextTransform
		if (pojo.getTextTransform() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getTextTransform(),
					StringParameter.UTF_8));
		}

		// Set LineHeight
		if (pojo.getLineHeight() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(sizeSelectIO.exportSPF(pojo.getLineHeight()));
		}

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected TextStyleTextModel doCreateObject() {
		return new TextStyleTextModel();
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			TextStyleTextModel pojo, IAppDataManager appDataManager) {
		return;
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			TextStyleTextModel pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set TextIndent
		Parameter indent = sei.getParameter(0);
		pojo.setTextIndent(sizeSelectIO.importSPF(indent));

		// Set TextAlign
		Parameter align = sei.getParameter(1);
		if (align.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) align;
			pojo.setTextAlign(s.toUTF8());
		}

		// Set TextDecoration
		Parameter decoration = sei.getParameter(2);
		if (decoration.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) decoration;
			pojo.setTextDecoration(s.toUTF8());
		}

		// Set LetterSpacing
		Parameter letter = sei.getParameter(3);
		pojo.setLetterSpacing(sizeSelectIO.importSPF(letter));

		// Set WordSpacing
		Parameter word = sei.getParameter(4);
		pojo.setWordSpacing(sizeSelectIO.importSPF(word));

		// Set TextTransform
		Parameter transform = sei.getParameter(5);
		if (transform.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) transform;
			pojo.setTextTransform(s.toUTF8());
		}

		// Set LineHeight
		Parameter lineheight = sei.getParameter(6);
		pojo.setLineHeight(sizeSelectIO.importSPF(lineheight));
	}
}
